/***********************************************
 This file is part of redia-bapps-main-ios.
 
 Copyright (c) 2015 Redia A/S
 
 Redia-bapps-main-ios is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 Redia-bapps-main-ios is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with redia-bapps-main-ios.  If not, see <http://www.gnu.org/licenses/>.
 
 *********************************************** */

//
//  BAReviewsViewController.m
//  redia-bapps-ios
//
//  Created by Mads Darø Kristensen on 10/23/13.
//  Copyright (c) 2013 redia. All rights reserved.
//

#import "BAReviewsViewController.h"
#import "BACustomNavigationBar.h"
#import "BAUtils.h"
#import "MWFSlideNavigationViewController.h"
#import "BAReviewsCell.h"
#import "BAReview.h"
#import "BAWebBrowserViewController.h"
#import "BAAboutAuthorViewController.h"


@interface BAReviewsViewController ()

@end

@implementation BAReviewsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    RD_SET_EDGES_FOR_EXTENDED_LAYOUT_IOS7
    self.view.backgroundColor = BA_CUSTOM_NAVBAR_DEFAULT_GRAY_COLOR;
    BA_FLYOUT_MENU_ADD_BUTTON(BASpecialNavigationBarConfigurationDefault);
    
    if (RD_IS_IPAD) {
        self.headerLabel.font = BA_FONT_LIGHT(16);
    } else {
        BA_LABEL_SET_FONT_LIGHT(self.headerLabel);
    }
    
    [self.tableView registerNib:[UINib nibWithNibName:@"BAReviewsCell" bundle:nil] forCellReuseIdentifier:@"BAReviewsCell"];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self enableUserInteraction];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:animated];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setRelatedMaterialsData:(BARelatedMaterialsData *)relatedMaterialsData
{
    _relatedMaterialsData = relatedMaterialsData;
    if ([self isViewLoaded]) {
        [self.tableView reloadData];
    }
}

#pragma mark - Flyout menu

BA_FLYOUT_MENU_ADD_CALLBACK

- (void)enableUserInteraction
{
    self.view.userInteractionEnabled = YES;
    self.tabBarController.tabBar.userInteractionEnabled = YES;
}

- (void)disableUserInteraction
{
    self.view.userInteractionEnabled = NO;
    self.tabBarController.tabBar.userInteractionEnabled = NO;
}


#pragma mark - Table view

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.relatedMaterialsData) {
        return [self.relatedMaterialsData.reviews count];
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 125;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BAReviewsCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"BAReviewsCell" forIndexPath:indexPath];
    BAReview *review = [self.relatedMaterialsData.reviews objectAtIndex:indexPath.row];

    if (review) {
        cell.sourceLabel.text = [NSString stringWithFormat:@"%@ (%@)", review.reviewCreator, [review.source capitalizedString]];
        if ([review.url length]>0) {
            cell.contentTextView.text = review.reviewDescription;
        } else {
            cell.contentTextView.text = review.text;
        }
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BAReview *review = [self.relatedMaterialsData.reviews objectAtIndex:indexPath.row];
    if ([review.url length]>0) {
        BAWebBrowserViewController *browser = [[BAWebBrowserViewController alloc] initWithNibName:@"BAWebBrowserViewController" bundle:nil];
        browser.url = review.url;
        [self presentViewController:browser animated:YES completion:NULL];
        
    } else {
        BAAboutAuthorViewController* aboutVC = [BAAboutAuthorViewController new];
        BARelatedMaterialsData* data = [BARelatedMaterialsData new];
        data.relationType = BA_RELATED_MATERIAL_TYPE_SINGLE_REVIEW;
        data.singleReview = review;
        [aboutVC setRelatedMaterialsData:data];
        [self.navigationController pushViewController:aboutVC animated:YES];
        
    }
}

@end
